package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.service.AuthService;
import com.cheer.service.MallProductService;
import com.cheer.vo.MallProductFrontInfoVO;
import com.cheer.vo.MallProductFrontListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.client.methods.HttpPut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author cheer
 */
@Api(tags = "产品")
@RestController
@RequestMapping("/product")
public class MallProductController {

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private AuthService authService;

    @ApiOperation(value = "商品列表 小红花 TODO")
    @GetMapping("/list")
    public Result<Page<MallProductFrontListVO>> list(Integer pageNum, Integer pageSize,
                                                     @RequestParam Long categoryId,
                                                     HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallProductService.listByCategoryId(pageNum, pageSize, categoryId));
    }

    @ApiOperation("根据关键词搜索商品列表")
    @GetMapping("/search")
    public Result<Page<MallProductFrontListVO>> search(Integer pageNum, Integer pageSize,
                                                       String keyword,
                                                       HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallProductService.search(pageNum, pageSize, keyword));
    }

    @ApiOperation("商品详情 小红花 TODO")
    @GetMapping("/info")
    public Result<MallProductFrontInfoVO> info(@RequestParam Long id,
                                               HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallProductService.frontInfo(id));
    }

}
